<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="类型" clearable style="width: 240px">
          <el-option v-for="dict in sys_banner_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="状态" clearable style="width: 240px">
          <el-option v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['mobile:banner:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['mobile:banner:remove']">删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" height="800" :data="bannerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" v-if="false" />

      <el-table-column label="轮播图类型" align="center" prop="type">
        <template #default="scope">
          <dict-tag :options="sys_banner_type" :value="scope.row.type" />
        </template>
      </el-table-column>

      <el-table-column label="图片" align="center" prop="url">
        <template #default="scope">
          <img :src="scope.row.url" min-width="70" height="70" />
        </template>
      </el-table-column>


      <el-table-column label="显示顺序" align="center" prop="orderNum" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['mobile:banner:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['mobile:banner:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改轮播图对话框 -->
    <el-dialog :title="title" v-model="open" :width="dialogWidth" append-to-body>
      <el-form ref="bannerForm" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="类型" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio v-for="dict in sys_banner_type" :key=dict.value :label=dict.value>{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="图片" prop="url">
          <el-input type="hidden" v-model="form.url" placeholder="请输入图片" />
          <!-- <ImageUpload :modelValue="form.url ? [{name:'xx',url:form.url}]: []" :limit="1" @update="changeUpload" apiUrl="/common/uploadImg" /> -->
          <ImageUpload v-model="form.url" :data={type:5} :limit="1" apiUrl="/common/uploadImg" />

        </el-form-item>

        <el-form-item label="显示顺序" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入显示顺序" />
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.value">{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="banner">
import { listBanner, getBanner, delBanner, addBanner, updateBanner } from "@/api/mobile/banner";


const { proxy } = getCurrentInstance();
const { sys_banner_type, sys_normal_disable } = proxy.useDict("sys_banner_type", "sys_normal_disable");

const bannerList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);
const dialogWidth = ref("0")

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    type: null,
    status: null,
  },
  // 表单参数
  form: {},
  // 表单校验
  rules: {
    type: [
      { required: true, message: "类型不能为空", trigger: "change" }
    ],
    url: [
      { required: true, message: "图片不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data);


/** 查询轮播图列表 */
function getList() {
  loading.value = true;
  listBanner(queryParams.value).then(response => {
    bannerList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}
// 表单重置
function reset() {
  form.value = {
    id: null,
    redirect: null,
    miniProgramPage: null,
    type: null,
    url: null,
    redirectUrl: null,
    orderNum: null,
    status: "0",
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("bannerForm");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryForm");
  handleQuery();
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加轮播图";
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const id = row.id || ids.value
  getBanner(id).then(response => {
    response.data.type = String(response.data.type);
    console.log(response.data)
    form.value = response.data;
    open.value = true;
    title.value = "修改轮播图";
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["bannerForm"].validate(valid => {
    const url = form.value.url ? form.value.url[0]?.url || null : null;
    if (valid) {
      if (form.value.id != null) {
        updateBanner({
          ...form.value,
          url
        }).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBanner({
          ...form.value,
          url
        }).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除数据项？').then(function () {
    return delBanner(ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download('mobile/banner/export', {
    ...queryParams.value
  }, `banner_${new Date().getTime()}.xlsx`)
}

function setDialogWidth() {
  var val = document.body.clientWidth
  const def = 500 // 默认宽度
  if (val < def) {
    dialogWidth.value = '100%'
  } else {
    dialogWidth.value = def + 'px'
  }
}

onMounted(() => {
  window.onresize = () => {
    return (() => {
      setDialogWidth()
    })()
  }
})

getList();
setDialogWidth();
</script>
